package com.android.mainapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MainAppService extends Service {// 继承Service, 它是Service的子类 用来实现AIDL通信
    private String TAG = "AIDL-MainAppService";
    private String mStrData;
    private boolean mSetServiceRunning = true;

    public MainAppService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v(TAG, "wcx MainAppService onCreate()");
    }

//    @Override
//    public void onStart(Intent intent, int startId) {
//        Log.v(TAG, "wcx MainAppService onStart()");
//    }

    IAppAidlInterface.Stub mStub = new IAppAidlInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
        }

        @Override
        public void setStringData(String strData) {
            mStrData = strData;
            Log.v(TAG, "wcx service mStub 调用setStringData函数=" + strData);
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        Log.v(TAG, "wcx service onBind() 成功返回 mStub对像");
        mSetServiceRunning = true;
        new Thread() { // 创建一个线程当绑定成功了就循环打印数据
            @Override
            public void run() {
                super.run();
                while (mSetServiceRunning) {
                    try {
                        Thread.sleep(1000); // 休眠1秒
                        Log.v(TAG, "wcx service mStrData:" + mStrData); // 打印数据
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        return mStub; //
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.v(TAG, "wcx service onUnbind()");
        mSetServiceRunning = false;
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v(TAG, "wcx service onDestroy()");
    }
}